package com.fitns.user.service.baseConfig;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fitns.user.conf.Constants;
import com.fitns.user.conf.ExcelConstants;
import com.fitns.user.dao.baseConfig.TbMiddleLedgerDao;
import com.fitns.user.dto.baseConfig.TbMiddleLedgerDTO;
import com.fitns.user.dto.baseConfig.TbMiddleLedgerVO;
import com.fitns.user.model.baseConfig.TbMiddleLedger;
import com.fitns.user.utils.DateUtil;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.lang.Validator;

@Service
public class TbMiddleLedgerService {

	@Autowired
	private TbMiddleLedgerDao tbMiddleLedgerDao;
	
	/**
	 * 新增中转站台账
	 * @param tbMiddleLedgerDTO
	 * @return
	 */
	@Transactional
	public Integer insertMiddleLedger(TbMiddleLedgerDTO tbMiddleLedgerDTO) {
		TbMiddleLedger record = new TbMiddleLedger();
		BeanUtils.copyProperties(tbMiddleLedgerDTO, record);
		record.setBeginTime(new Date());
	    return tbMiddleLedgerDao.insertSelective(record);
	}
	
	/**
	 * 修改中转站台账
	 * @param tbMiddleLedgerDTO
	 * @return
	 */
	@Transactional
	public Integer updateMiddleLedger(TbMiddleLedgerDTO tbMiddleLedgerDTO) {
		TbMiddleLedger record = new TbMiddleLedger();
		BeanUtils.copyProperties(tbMiddleLedgerDTO, record);
		record.setId(tbMiddleLedgerDTO.getId());
		record.setBeginTime(new Date());
		return tbMiddleLedgerDao.updateMiddleLedger(record);
	}
	
	/**
	 * 根据id删除中转站台账
	 * @param tbMiddleLedgerDTO
	 * @return
	 */
	@Transactional
	public Integer deleteMiddleLedgerById(long id) {
	 	return tbMiddleLedgerDao.deleteMiddleLedgerById(id);
	}
	
	/**
	 * 查询
	 * @param tbMiddleLedgerDTO
	 * @return
	 */
	public  PageInfo<TbMiddleLedgerVO> selectMiddleLedger(TbMiddleLedgerDTO tbMiddleLedgerDTO){
		tbMiddleLedgerDTO.pageHandle();
		List<TbMiddleLedgerVO> select = tbMiddleLedgerDao.selectMiddleLedger(tbMiddleLedgerDTO);
		PageInfo<TbMiddleLedgerVO> selectList = new PageInfo<>(select);
		return selectList;
	}
	
	public TbMiddleLedger selectMiddleById(long id) {
		return tbMiddleLedgerDao.selectByPrimaryKey(id);
	}
	
	
	
	@Transactional
	public Integer inserIcon(TbMiddleLedgerDTO tbMiddleLedgerDTO) {
		TbMiddleLedger record  = new TbMiddleLedger();
		BeanUtils.copyProperties(tbMiddleLedgerDTO, record);
		if(Validator.isNull(tbMiddleLedgerDTO.getId())){
			
			return this.insertMiddleLedger(tbMiddleLedgerDTO);
		}else {
			record.setImage(tbMiddleLedgerDTO.getImage());
			record.setModifyTime(new Date());
			return tbMiddleLedgerDao.updateByPrimaryKey(record);
		}

	}
	


	   public List<List<String>> export(TbMiddleLedgerDTO tbMiddleLedgerDTO){
	        List<TbMiddleLedger> facilityDataList=tbMiddleLedgerDao.searchForExport(tbMiddleLedgerDTO);
	        List<List<String>> excelList = new ArrayList<>();
	        excelList.add(this.getRowHeader(ExcelConstants.MIDDLE_LEDGER_IMPORT_HEADER.class));
	        for (int i = 0; i < facilityDataList.size(); i++) {
	                
	            excelList.add(this.getrow(facilityDataList.get(i),i));
	        }
	        return excelList;
	    }
	    
	    public List<String> getRowHeader(Class clazz){
	        Field[] fields=clazz.getDeclaredFields();
	        List<String> rowHeader = new ArrayList<>();
	        for(int i=0;i<fields.length-1;i++){
	            try {
	                Object object =fields[i].get(clazz);
	                String value = object.toString();
	                rowHeader.add(value);
	            } catch (IllegalArgumentException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            }
	        }
	        return rowHeader;
	    }

	    public List<String> getrow(TbMiddleLedger tbMiddleLedger, int i){
	        List<String> row = new ArrayList<>();
	       
	        row.add(String.valueOf(i + 1));
	        //if else处理的是数据库要导出的数据中不为string的字段，
	        //时间格式的就是这样的写法，因为工具类中已经对时间处理了，固定 的写法
	        if(Validator.isNull(tbMiddleLedger.getBeginTime())) {
	            row.add("");
	        }else {
	            row.add(DateUtil.parseDateToStr(tbMiddleLedger.getBeginTime(),Constants.DATESTR));
	        }
	       
	        if(Validator.isNull(tbMiddleLedger.getPopulation())) {
	            row.add("");
	        }else {
	            row.add(String.valueOf(tbMiddleLedger.getPopulation()));
	        }
	        if(Validator.isNull(tbMiddleLedger.getAcreage())) {
	            row.add("");
	        }else {
	            row.add(String.valueOf(tbMiddleLedger.getAcreage()));
	        }
	        row.add(tbMiddleLedger.getName());
	        row.add(tbMiddleLedger.getAddress());
	        row.add(tbMiddleLedger.getStatus());
	        row.add(tbMiddleLedger.getCounter());
	        row.add(tbMiddleLedger.getLiabler());
	        row.add(tbMiddleLedger.getPhone());
	        row.add(tbMiddleLedger.getTerminal());
	        row.add(tbMiddleLedger.getRfid());
	        row.add(tbMiddleLedger.getImage());
	        row.add(tbMiddleLedger.getDescs());
	       return row;
	    }



}
